<?php
/**
 * Created by PhpStorm.
 * User: man0sions
 * Date: 16/8/25
 * Time: 下午9:36
 */

namespace src\base;


use src\base\api\Command;

/**
 * 抓取抽象接口,需要实现doSpider 功能
 *
 * Class Spider
 * @package src\base
 */

abstract class Spider implements Command
{
    protected $config;
    protected $state = [];

    /**
     * @param Context $context
     */
    function execute(Context $context)
    {
        $this->config = $context->get('spider');
        $spider = $context->get('config');

        foreach($spider['urls'] as $item)
        {
            for($page=1;$page<=$this->config['page'];$page++)
            {
                $url = strtr($item['url'],[
                    '{page}'=>$page
                ]);
                echo "====>$url\n";

               $ret = $this->doSpider($url,$item['firstcategoryid']);
                if($ret===false)
                    break;

            }
        }

    }

    /**
     * @param $url
     * @param $firstcategoryid
     * @return mixed
     */
    abstract function doSpider($url,$firstcategoryid);

}